﻿namespace WMS6.Web.Billing
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoServiceRef;

    public class Rate : BasePage
    {
        protected Button btnAddDetail;
        protected Button btnCopy;
        protected Button btnDelete;
        protected Button btnDeleteDetail;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnSave;
        protected CheckBox chkIsActive;
        protected CheckBox chkSystemCreated;
        protected HtmlForm F9051;
        protected GridView gvRateDetail;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnLineID;
        protected HtmlInputHidden hdnRateID;
        protected HtmlHead Head1;
        protected Label lblDescr;
        protected Label lblMsg;
        protected Label lblOwner;
        protected Label lblRateID;
        protected Label lblRateName;
        protected HtmlLink mycss;
        protected Label one1;
        protected Label one2;
        protected ScriptManager ScriptManager1;
        protected TextBox txtDescr;
        protected TextBox txtRateID;
        protected TextBox txtRateName;
        protected UpdatePanel up1;

        protected void btnAddDetail_Click(object sender, EventArgs e)
        {
            if (this.hdnFlag.Value != "1")
            {
                this.InsertRateInfo();
            }
            base.Response.Redirect(string.Format("RateDetail.aspx?from={0}&ID={1}&LineID={2}", base.UrlEncode(this.hdnFrom.Value), base.UrlEncode(this.txtRateID.Text.Trim()), base.UrlEncode(this.hdnLineID.Value)), true);
        }

        protected void btnCopy_Click(object sender, EventArgs e)
        {
            this.txtRateID.Text = "";
            this.txtRateID.Enabled = true;
            base.DataEntityViewState = null;
            this.hdnFlag.Value = "";
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.chkSystemCreated.Checked)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9150_007");
            }
            else if (!string.IsNullOrEmpty(this.txtRateID.Text.Trim()))
            {
                try
                {
                    List<string> RateIDS = new List<string> {
                        this.txtRateID.Text.Trim()
                    };
                    this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Delete(RateIDS, "RATE") + base.GetGlobalMsgResource("F9150_008");
                    base.DataEntityViewState = null;
                    this.btnCopy.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnSave.Enabled = false;
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F9150_009") + ex.Message;
                }
            }
        }

        protected void btnDeleteDetail_Click(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvRateDetail, "LineID");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9150_010");
            }
            else
            {
                List<string> initLocal0 = new List<string> {
                    this.txtRateID.Text
                };
                this.lblMsg.Text = BaseInfoDetailServiceClientProxy.GetInstance().DeleteDetail(initLocal0, selectedID, new List<string>(), "RATE_DETAIL") + base.GetGlobalMsgResource("F9150_008");
                this.SetRateInfo2Form();
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,2) </script>", false);
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("RateQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("RateList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            base.Server.Transfer(string.Format("Rate.aspx?from={0}", base.UrlEncode(this.hdnFrom.Value.Trim())), false);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtRateName.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9150_002");
            }
            else if (this.hdnFlag.Value == "1")
            {
                this.UpdateRateInfo();
            }
            else
            {
                this.InsertRateInfo();
            }
        }

        protected void gvRateDetail_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F9051", this.gvRateDetail);
        }

        protected void gvRateDetail_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            (sender as GridView).PageIndex = e.NewPageIndex;
            (sender as GridView).DataSource = (base.DataEntityViewState as RateInfo).RateDetailInfos;
            (sender as GridView).DataBind();
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,2) </script>", false);
        }

        protected void gvRateDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int i = this.gvRateDetail.PageIndex * this.gvRateDetail.PageSize;
                e.Row.Cells[1].Text = ((e.Row.RowIndex + 1) + i).ToString();
                base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblDescr"));
            }
        }

        protected void gvRateDetail_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                string sortDirection = string.Empty;
                if (base.GridViewSortDirection == SortDirection.Ascending)
                {
                    base.GridViewSortDirection = SortDirection.Descending;
                    sortDirection = "DESC";
                }
                else
                {
                    base.GridViewSortDirection = SortDirection.Ascending;
                    sortDirection = "ASC";
                }
                RateInfo rateInfo = base.DataEntityViewState as RateInfo;
                DataView dv = CADataConverter.ToDataTable(rateInfo.RateDetailInfos).DefaultView;
                dv.Sort = e.SortExpression + " " + sortDirection;
                base.BindData2GridView(this.gvRateDetail, dv);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,2);</script>", false);
            }
        }

        private void InsertRateInfo()
        {
            try
            {
                RateInfo rateInfo = new RateInfo {
                    RateID = this.txtRateID.Text,
                    RateName = this.txtRateName.Text,
                    Descr = this.txtDescr.Text,
                    IsActive = this.chkIsActive.Checked ? "Y" : "N",
                    SystemCreated = this.chkSystemCreated.Checked ? "Y" : "N",
                    CreatedBy = base.UserInfo.UserID
                };
                this.hdnRateID.Value = BaseInfoServiceClientProxy.GetInstance().Insert(rateInfo, "RATE");
                this.hdnLineID.Value = "10";
                this.hdnFlag.Value = "1";
                base.DataEntityViewState = rateInfo;
                this.txtRateID.Text = this.hdnRateID.Value;
                this.txtRateID.Enabled = false;
                this.lblMsg.Text = this.hdnRateID.Value + base.GetGlobalMsgResource("F9150_005");
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9150_006") + ex.Message;
            }
        }

        private void LoadData()
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnFlag.Value = "1";
                this.hdnRateID.Value = base.Request.QueryString["ID"];
                this.SetRateInfo2Form();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["detail"]))
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,2) </script>", false);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetRateInfo2Form()
        {
            try
            {
                RateInfo rateInfo = (RateInfo) BaseInfoServiceClientProxy.GetInstance().GetInfoByID(this.hdnRateID.Value, "RATE");
                if (rateInfo != null)
                {
                    this.txtRateID.Text = rateInfo.RateID;
                    this.txtRateID.Enabled = false;
                    this.txtRateName.Text = rateInfo.RateName;
                    this.txtDescr.Text = rateInfo.Descr;
                    this.chkIsActive.Checked = rateInfo.IsActive == "Y";
                    this.chkSystemCreated.Checked = rateInfo.SystemCreated == "Y";
                    if ((rateInfo.RateDetailInfos != null) && (rateInfo.RateDetailInfos.Count > 0))
                    {
                        this.hdnLineID.Value = (rateInfo.RateDetailInfos[rateInfo.RateDetailInfos.Count - 1].LineID + 10).ToString();
                    }
                    else
                    {
                        this.hdnLineID.Value = "10";
                    }
                    base.BindData2GridView(this.gvRateDetail, rateInfo.RateDetailInfos);
                    base.DataEntityViewState = rateInfo;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void UpdateRateInfo()
        {
            try
            {
                RateInfo rateInfo = base.DataEntityViewState as RateInfo;
                rateInfo.RateName = this.txtRateName.Text;
                rateInfo.Descr = this.txtDescr.Text;
                rateInfo.IsActive = this.chkIsActive.Checked ? "Y" : "N";
                rateInfo.SystemCreated = this.chkSystemCreated.Checked ? "Y" : "N";
                rateInfo.UpdatedBy = base.UserInfo.UserID;
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Update(rateInfo, "RATE") + base.GetGlobalMsgResource("F9150_003");
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9150_004") + ex.Message;
            }
        }
    }
}

